package com.kehao.leetcode.middle.array;


import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class MatrixSetZero {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(matrix[i][j]==0){
                    list.add(new int[]{i,j});
                }
            }
        }
        for (int[] numArray :list){
            for (int j = 0; j < n; j++) {
                matrix[numArray[0]][j]=0;
            }
            for (int i = 0; i < m; i++) {
                matrix[i][numArray[1]]=0;
            }
        }
    }

    @Test
    public void test01(){
        int[][] matrix = {{1,1,1},{1,0,1},{1,1,1}};
        setZeroes(matrix);

        int m = matrix.length;
        int n = matrix[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrix[i][j]);
            }
            System.out.println();
        }
    }

    @Test
    public void test02(){
        int[][] matrix = {{0,1,2,0},{3,4,5,2},{1,3,1,5}};
        setZeroes(matrix);

        int m = matrix.length;
        int n = matrix[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrix[i][j]);
            }
            System.out.println();
        }
    }
}
